const app = getApp();
const config = require('../../../config/config.js');
const util = require('../../../utils/util.js');

Page({
  data: {
    lists: [],
    type:'',
    isFinish: ''
  },
  onLoad: function (e) {
    if(e.type){
      if(e.type === 'select'){
        this.data.isFinish = 1;
        wx.setNavigationBarTitle({
          title: '选择车辆信息'
        });
      }
      this.setData({type:e.type});
    }
  },

  onShow: function () {
    const postData = this.data.isFinish ? { userId: app.globalData.userId, page: 1, pageSize: 10, isFinish: this.data.isFinish } : { userId: app.globalData.userId, page: 1, pageSize: 10 }
    app.request('/Car/CarList', 'GET', postData,
      res => {
        res.list.forEach(item => {
          item.activeDotList = util.createDotList(3, item.step);
        });
        this.setData({
          lists: res.list
        });
      }, '获取车辆信息失败');
  },
  onSelectCar:function(e){
    const index = e.currentTarget.dataset.index;
    app.globalData.carInfo = this.data.lists[index]; 
    wx.navigateBack();
  },

  deleteLists: function (e) {
    var that = this;
    var lists = this.data.lists;
    var index = e.currentTarget.dataset.index;
    wx.showModal({
      title: '提示',
      content: '确定要删除吗？',
      success: function (res) {
        if (res.confirm) {
          console.log('点击确定了');
          app.request('/Car/Delete', 'DELETE', {
            userId:app.globalData.userId,
            carId:e.currentTarget.id
          },
            res => {
              wx.showToast({
                title: '删除成功',
                success: res =>{
                  lists.splice(index, 1);
                  that.setData({
                    lists: lists,
                  });
                }
              })
            }, '删除车辆失败');           
        } else if (res.cancel) {
          console.log('点击取消了');
          return false;
        }
      }
    })
  },
  addList: function () {
    wx.navigateTo({url: '../create/create'});
  },

  gotoDetail: function (e) {
    console.log(e);
    wx.navigateTo({ url: `../create/create?carId=${e.currentTarget.id}`});
  }

})